package interfaces.menu.options;

import java.util.ArrayList;
import java.util.List;

import logic.nodes.nodeSettings.Settings;

import fileHandling.language.Lang;
import fileHandling.language.interfaces.OptionsWindowText;
import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.menu.AbsMenu;
import interfaces.options.OptionWidget;
import interfaces.options.OptionWidgetFactory;
import interfaces.options.OptionsContainer;
import interfaces.options.TextInputError;
import interfaces.options.TextInputErrorWindow;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.StaticContent;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

public abstract class OptionContent extends StaticContent {
	
	protected OptionsMenu optionsMenu;
	protected LanguageButton applyButton;
	protected List<InterfaceButton> tabButtons;
	protected List<OptionsContainer> optionsContainers;
	protected AbsMenu menu;
	protected OptionWidgetFactory widgetFactory;
	protected Settings settings;
	
	protected ImageFont applyButtonFont;
	protected int applyButtonHeight;
	
	public OptionContent(OptionsMenu optionsMenu, AbsMenu menu, String fileName, Settings settings) {
		super(optionsMenu.getOptionContentSize());
		this.optionsMenu = optionsMenu;
		this.menu = menu;
		this.settings = settings;
		
		widgetFactory = new OptionWidgetFactory(fileName, this);
		
		optionsContainers = new ArrayList<OptionsContainer>();
		tabButtons = new ArrayList<InterfaceButton>();
		
		applyButtonFont = GUISource.bigFont;
		applyButtonHeight = applyButtonFont.getHeight();
		
		layout();
	}
	
	protected abstract void apply();
	
	protected OptionsContainer createOptionsContainer(Lang tabText, Lang... options) {
		OptionsContainer container = getNewOptionContainer();
		addTabButton(tabText, container);
		
		for(Lang o : options) {
			OptionWidget widget = widgetFactory.createWidget(o, settings.getValueOf(o));
			addOptionWidget(widget, container);
		}
		
		return container;
	}
	
	protected void addOptionWidget(OptionWidget widget, OptionsContainer container) {
		container.addOption(widget);
	}
	
	private LanguageButton createApplyButton() {
		applyButton = new LanguageButton(OptionsWindowText.Apply, applyButtonFont);
		applyButton.setSize(width, applyButtonHeight);
		applyButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				for(OptionsContainer o : optionsContainers) {
					TextInputError error = o.checkTextInput();
					if(error != null) {
						TextInputErrorWindow window = new TextInputErrorWindow(error, menu);
						menu.addWidget(window);
						return;
					}
				}
				apply();
			}
		});
		return applyButton;
	}
	
	protected OptionsContainer getNewOptionContainer() {
		int optionWidth = width;
		int optionHeight = height - applyButtonHeight;
		OptionsContainer o = new OptionsContainer(optionWidth, optionHeight, 0, applyButtonHeight);
		optionsContainers.add(o);
		return o;
	}
	
	protected void addTabButton(Lang buttonLabel, final OptionsContainer container) {
		LanguageButton button = new LanguageButton(buttonLabel, GUISource.middleFont);
		
		tabButtons.add(button);
		
		StaticContent content = new StaticContent(getSize());
		content.setXY(0, 0);
		content.addWidget(container);
		content.addWidget(createApplyButton());
		content.layout();
		
		optionsMenu.getOptionSwitchContent().addSwitchContent(button, content);
	}
	
	public AbsMenu getMenu() { return menu; }
	
	public List<InterfaceButton> getTabButtons() { return tabButtons; }
}